package com.glsc.ngateway.platform.repo;

import com.glsc.ngateway.common.api.flowable.dto.FlowParamAuthDto;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.VEhrAdminStaff;
import com.glsc.ngateway.platform.domain.TestTask;
import com.glsc.ngateway.platform.dto.TestTaskDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface TestTaskRepository extends JpaRepository<TestTask, Long>, JpaSpecificationExecutor<TestTask> {



    @Query(value = "select m from TestTask m where m.processId = :processId")
    TestTask queryByProcessId(@Param("processId") String  processId);

    @Query(value = "select * from t_test_task p where 1=1 " +
            "and (:#{#paramSearchDto.processTitle} is null OR p.vc_process_title LIKE CONCAT('%', :#{#paramSearchDto.processTitle}, '%')) " +
            "and (:#{#paramSearchDto.systemCode} is null OR p.system_code LIKE CONCAT('%', :#{#paramSearchDto.systemCode}, '%')) " +
            "and (coalesce(:#{#paramSearchDto.systemCodes}, null) is null or p.system_code in (:#{#paramSearchDto.systemCodes}))" +
            "and (:#{#paramSearchDto.flowCreateOperatorAccount} is null OR p.flow_create_operator_account LIKE CONCAT('%', :#{#paramSearchDto.flowCreateOperatorAccount}, '%')) " +
            "and (coalesce(:#{#paramSearchDto.requireProcessIds}, null) is null or p.vc_process_id in (:#{#paramSearchDto.requireProcessIds}))" +
            "and (:#{#paramSearchDto.beginDateLocalDate} is null OR p.dt_process_create_time >= :#{#paramSearchDto.beginDateLocalDate} )  " +
            "and (:#{#paramSearchDto.endDateLocalDate} is null OR p.dt_process_create_time <= :#{#paramSearchDto.endDateLocalDate} )  " +
            " and (COALESCE(:#{#authCondition.admin}, null) IS null " +
            "or " +
            " ( " +
            "p.vc_process_id IN (:#{#authCondition.processIds}) or " +
            " p.system_code in (select a.system_class_no from t_gw_system_member  me inner join t_gw_system_main a on me.op_id = a.op_id where 1=1 " +
            "            and me.deleted = 0 and me.member_type = '1' and me.status = '1' " +
            "            and me.member_account in (:staffs) ) " +
            "or p.flow_create_operator_account in (:staffs) " +
            "or p.system_manager_account in (:staffs)  " +
            "or p.vc_requirement_manager in (:staffs)  " +
            " ) " +
            " ) " +
            "order by p.dt_process_create_time desc" ,nativeQuery = true)
    Page<TestTask> searchByParam(Pageable pageable, TestTaskDto paramSearchDto, FlowParamAuthDto authCondition, List<VEhrAdminStaff> staffs);
}
